package com.snda.lib.util;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;

public class ApkHelper {

	public static boolean isValidApk(String strApkPath) {

		ZipFile zFile = null;
		try {
			zFile = new ZipFile(strApkPath);
		} catch (IOException e) {
		}

		if (zFile != null)
			return true;

		return false;
	}

	/**
	 * 获取应用版本号
	 * 
	 * @param context
	 * @param pkgName
	 * @return
	 */
	public static int getAppVersionCode(Context context, String pkgName) {
		PackageManager pm = context.getPackageManager();
		PackageInfo pkgInfo;
		try {
			pkgInfo = pm.getPackageInfo(pkgName, 0);
			if (pkgInfo != null) {
				return pkgInfo.versionCode;
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 检查是否是否存在给定的Activity
	 * 
	 * @param ctx
	 * @param strActivityName
	 * @return
	 */
	public static boolean existActivity(Context ctx, String strActivityName) {
		PackageManager packageManager = ctx.getPackageManager();

		try {
			PackageInfo pInfo = packageManager.getPackageInfo(
					ctx.getPackageName(), PackageManager.GET_ACTIVITIES
							| PackageManager.GET_PERMISSIONS);
			if (pInfo != null) {

				for (int i = 0; i < pInfo.activities.length; i++) {
					String strName = pInfo.activities[i].name;
					if (strName.equalsIgnoreCase(strActivityName))
						return true;
				}
			}

		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 检查是否存在给定的MetaData元素
	 * 
	 * @param ctx
	 * @param strMetaDataName
	 * @return
	 */
	public static boolean existMetaData(Context ctx, String strMetaDataName) {
		String strMetaName = getMetaData(ctx, strMetaDataName);
		if (strMetaName == null || strMetaName.trim().equals("") == true)
			return false;

		return true;
	}

	/**
	 * 获取特定的Meta元素的内容
	 * 
	 * @param ctx
	 * @param strMetaDataName
	 * @return
	 */
	public static String getMetaData(Context ctx, String strMetaDataName) {
		if (ctx == null)
			return null;

		PackageManager packageManager = ctx.getPackageManager();

		try {
			ApplicationInfo ai = packageManager.getApplicationInfo(
					ctx.getPackageName(), PackageManager.GET_META_DATA);

			if (ai != null) {

				Bundle bundle = ai.metaData;
				if (bundle == null)
					return null;

				String strName = bundle.getString(strMetaDataName);
				return strName;
			}

		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 检查是否存在特定的权限
	 * 
	 * @param ctx
	 * @param strPermissionName
	 * @return
	 */
	public static boolean existPermission(Context ctx, String strPermissionName) {
		PackageManager packageManager = ctx.getPackageManager();

		try {
			PackageInfo pInfo = packageManager.getPackageInfo(
					ctx.getPackageName(), PackageManager.GET_ACTIVITIES
							| PackageManager.GET_PERMISSIONS);
			if (pInfo != null && pInfo.requestedPermissions != null) {

				for (int i = 0; i < pInfo.requestedPermissions.length; i++) {
					String strName = pInfo.requestedPermissions[i];
					if (strName.equalsIgnoreCase(strPermissionName) == true)
						return true;
				}

			}

		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 安装给定路径的Apk
	 * 
	 * @param ac
	 * @param strApkPath
	 */
	public static void installApk(Activity ac, String strApkPath) {

		File downloadFile = new File(strApkPath);
		Uri uri = Uri.fromFile(downloadFile);
		Intent installIntent = new Intent(Intent.ACTION_VIEW);
		installIntent.setDataAndType(uri,
				"application/vnd.android.package-archive");
		ac.startActivity(installIntent);
	}
}
